<?php

/**
 * This is the model class for table "page".
 *
 * The followings are the available columns in table 'page':
 * @property integer $id
 * @property integer $category_id
 * @property string $title
 * @property string $prev_content
 * @property string $full_content
 * @property string $url
 * @property string $meta_title
 * @property string $meta_description
 * @property string $meta_keywords
 * @property string $image
 * @property integer $status
 * @property integer $position
 * @property integer $create_time
 * @property integer $public_time
 */
class Page extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Page the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{page}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, url', 'required'),
            array('title, url', 'unique'),
            array('url', 'match', 'pattern'=>'/^([A-Za-z0-9_ ]+)$/u', 'message'=>'Допустимые символы A-Z a-z 0-9 _ '),
			array('category_id, status, position', 'numerical', 'integerOnly'=>true),
			array('title, url, meta_title', 'length', 'max'=>255),
			array('image', 'length', 'max'=>50),
			array('prev_content, full_content, meta_description, meta_keywords, public_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, category_id, title, url, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'category'=>array(self::BELONGS_TO, 'Category', 'category_id'),   //Связываем таблицы page и category. 
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'category_id' => 'Категория',
			'title' => 'Название',
			'prev_content' => 'Предварительное содержание',
			'full_content' => 'Полное содержание',
			'url' => 'URL',
			'meta_title' => 'Meta Title',
			'meta_description' => 'Meta Description',
			'meta_keywords' => 'Meta Keywords',
			'image' => 'Изображение',
			'status' => 'Статус',
            'position'=>'Позиция',
			'create_time' => 'Дата и время создания',
			'public_time' => 'Дата публикации',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
        $criteria->order = 'position ASC';

		$criteria->compare('id',$this->id);
		$criteria->compare('category_id',$this->category_id);
		$criteria->compare('title',$this->title,true);
//		$criteria->compare('prev_content',$this->prev_content,true);
//		$criteria->compare('full_content',$this->full_content,true);
		$criteria->compare('url',$this->url,true);
//		$criteria->compare('meta_title',$this->meta_title,true);
//		$criteria->compare('meta_description',$this->meta_description,true);
//		$criteria->compare('meta_keywords',$this->meta_keywords,true);
//		$criteria->compare('image',$this->image,true);
		$criteria->compare('status',$this->status);
//		$criteria->compare('create_time',$this->create_time);
//		$criteria->compare('public_time',$this->public_time);


		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                'pagination'=>array(
				    'pageSize'=>20,
							),
            
		));
	}
    
    
    
    public function beforeSave()
    {
        if($this->isNewRecord)
            {
            $this->create_time = time();
            $this->url = DKUtils::changeUrl($this->url);
            }
            $this->position = DKUtils::maxposition("Page") + 1;
           $this->public_time = strtotime($this->public_time); 
        return parent::beforeSave();    
        
    }
    
    

    protected function afterFind() {
     $date = date('d-m-Y', $this->public_time);
     $this->public_time = $date;
     parent::afterFind();
    }
    
 
    
}